import axios from 'axios'
import qs from 'qs'
// import router from '../../router'
// axios.defaults.baseURL = 'http://10.107.10.80:8080/' // wrr
// axios.defaults.baseURL = 'http://10.107.11.208:8080/' // hmj
// axios.defaults.baseURL = 'http://10.112.231.178:8080/' // wrr
const baseUrlKang = 'https://122.112.236.124:443' // 康晓辉
const instance = axios.create()
instance.defaults.timeout = 10000
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
instance.interceptors.request.use((config) => {
  instance.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  return config
}, (error) => {
  return Promise.reject(error)
})

/* instance.interceptors.response.use((res) => {
  let isLogin = res.headers.islogin
  console.log('isLogin')
  console.log(res)
  if (isLogin === 'true') {
    return res
  } else {
    alert('登录验证过期')
    router.push('/login')
    return res
  }
}, (error) => {
  return Promise.reject(error)
}) */

export function fetch (url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, params)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

export default {
  /**
   * 登陆接口
   * **/
  toLogin (params) {
    return fetch(`${baseUrlKang}/airplane/interface/admin/login`, qs.stringify(params))
  },
  /**
   * 退出登陆接口
   * **/
  logOut (params) {
    return fetch(`${baseUrlKang}/airplane/interface/admin/logout`, qs.stringify(params))
  },
  // 获取侧边栏
  getSideItems (params) {
    return fetch(`${baseUrlKang}/airplane/interface/permission/query_menu`, qs.stringify(params))
  },
  checkPassWord (params) {
    return fetch('/logistics/interface/loginManagement/verify_password', qs.stringify(params))
  }
}
